//
//  FeedbackViewController.m
//  Kjs-iOS
//
//  Created by Pat on 16/8/5.
//  Copyright © 2016年 jmhz. All rights reserved.
//

#import "FeedbackViewController.h"
#import "KJSRequest.h"
#import "AppDelegate.h"
#import "MBProgressHUD+MAP.h"
#import "GUATool.h"

@interface FeedbackViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UIButton *submitBtn;
@property (strong, nonatomic) NSMutableAttributedString *placeholder;
@end

@implementation FeedbackViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"意见反馈";
    
    //确认按钮圆角和事件处理
    self.submitBtn.layer.cornerRadius = self.submitBtn.frame.size.height/2;
    [self.submitBtn addTarget:self action:@selector(submitFeedback) forControlEvents:UIControlEventTouchUpInside];
    
    //textview 样式处理
    self.textView.delegate = self;
    [self textViewStyleConfig];
}

- (NSMutableAttributedString *)placeholder {
    if (!_placeholder) {
        _placeholder = [[NSMutableAttributedString alloc] initWithString:@"感谢您的意见！如果您的问题需要马上反馈，请联系快金所客服 0591-83978755"];
        [_placeholder addAttribute:NSForegroundColorAttributeName
                             value:[UIColor colorWithRed:204.0/255 green:204.0/255 blue:204.0/255 alpha:1.0] //204,204,204 即 #cccccc
                             range:NSMakeRange(0, _placeholder.length)];
    }
    return _placeholder;
}


- (void)submitFeedback {
    if ([GUATool isEmptyString:self.textView.text] || [self.textView.textStorage.string isEqualToString:self.placeholder.string]){
        //反馈信息判空
        [MBProgressHUD showError:@"反馈信息不能为空!"];
        return;
    }
    [CurentDelegate getAccountMessageAt:self ret:^(UserInfo *account) {
        [KJSRequest feedbackToAdminWithPartyId:account.partyId
                                       Content:self.textView.text
                                        Source:@"2"
                                           ret:^(BasicResult *model) {
                                               if (model.success) {
                                                   [MBProgressHUD showSuccess:@"感谢您的反馈！"];
                                                   [self.navigationController popViewControllerAnimated:YES];
                                               } else {
                                                   [MBProgressHUD showError:@"提交反馈失败，请稍后重试。"];
                                               }
                                           }];
    }];
}

- (void)textViewStyleConfig {
    //设置 placeholder
    self.textView.attributedText = self.placeholder;
    self.textView.layer.borderWidth = 1.f;
    //221,221,221 即 #dddddd
    self.textView.layer.borderColor = [UIColor colorWithRed:221.0/255
                                                      green:221.0/255
                                                       blue:221.0/255
                                                      alpha:1.0].CGColor;
    self.textView.textContainerInset = UIEdgeInsetsMake(8, 4, 8, 4);    //左右方向原本就有约 4px 的 inset
}

//以下两个方法实现 textview 的 placeholder
- (void)textViewDidEndEditing:(UITextView *)textView {
    if (textView.text.length < 1) {
        self.textView.attributedText = self.placeholder;
    }
}

- (void)textViewDidBeginEditing:(UITextView *)textView {
    if ([self.textView.textStorage.string isEqualToString:self.placeholder.string]) {
        //要先改颜色, 否则 attributedText 长度为0的时候改颜色不起作用
        [self.textView.textStorage addAttribute:NSForegroundColorAttributeName
                                          value:[UIColor colorWithRed:51.0/255 green:51.0/255 blue:51.0/255 alpha:1.0] //51,51,51 即 #333333
                                          range:NSMakeRange(0, self.textView.textStorage.string.length)];
        self.textView.attributedText = [[NSMutableAttributedString alloc] initWithString:@""];
    }
}
@end
